package com.yanyu.space.common.sentinel.handler;

import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.yanyu.space.common.core.bean.web.RCode;
import com.yanyu.space.common.core.util.WebUtil;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author gz
 */
@Slf4j
public class SentinelGatewayExceptionHandler implements BlockExceptionHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, BlockException e) throws Exception {
        log.error("Blocked by Sentinel (flow limiting)", e);
        WebUtil.out(response, RCode.ACCESS_TOO_MANY);
    }
}
